
	/***********************************************************************************************

		Class: jsl.Module extends jsl.EventMap, jsl.OptionMap
		
		Methods:
			obj			jsl.Module 	(id) 											- sid: 3.0
			obj			addInfo			(id, value*, getter*)			- sid: 3.0
			info		getInfo			(id)											- sid: 3.0
			b				isInfo			(id)											- sid: 3.0
			data		getInfoData	(id)											- sid: 3.0

		Legend:
			data { id, name, parent, value, getter }
 			
	************************************************************************************************/
	
	//-------------------------------------------------------------------------------
	jsl.Class("jsl.Module", 
		{
			//-------------------------------------------------------------------------------
			inherits: [ jsl.EventMap, jsl.OptionMap ],
			
			//Private vars			
			//-------------------------------------------------------------------------------
			__infos: null,
			
			//-------------------------------------------------------------------------------
			Module: function(id) {
				//@d[
				if(arguments.length !== 1) 			throw jsl.ParamError()
				jsl.validate(id, "qid", "id")
        if(jsl.__mods[id])							throw jsl.ParamError("AlreadyDefined:id")
        //@d]
				
				this.OptionMap()
				this.EventMap()
				this.setName(id)

				this.__infos 		= []
				jsl.__mods[id]	= this
				jsl.__mods.push(this)
			},
      //-------------------------------------------------------------------------------
			addInfo: function(id, value, getter) {
				var parent, pid, name, i
				
				//@d[
				if(!arguments.length || arguments.length > 3)		      throw jsl.ParamError()
				jsl.validate(id, "s+", "id")
				if(this.__infos["i:" + id])														throw jsl.ParamError("AlreadyDefined:id")
				if(arguments.length === 3) 														jsl.validate(getter, "f", "getter")
				//@d]

				if(id.indexOf(".") !== -1) {
					pid  		= id.slice(0, id.lastIndexOf("."))
          name    = id.slice(id.lastIndexOf(".") + 1)
					parent	= this.__infos["i:" + pid]
          if(!parent)                    											throw jsl.ParamError("NotDefined: id(parent-id)")
          if(!jsl.isObject(this.__infos["i:" + pid]))					throw jsl.ParamError("BadType:    id(parent-id):object")
				}
        else
          name = id
          
				i =	{
					id: 		id 		+ "" 	,
          name:   name  + ""  ,
          parent: parent 	|| null,
					getter:	arguments.length === 2 && jsl.isFunction(value) ? value : getter  || null,
					value: 	arguments.length === 2 && jsl.isFunction(value) ? null	: (arguments.length >= 2 ? value : {}),
					childs:	[]
				}

				this.__infos["i:" + id] = i
				this.__infos.push(i)
				if(parent) parent.childs.push(i)
				
				return this
			},
			//-------------------------------------------------------------------------------
			getInfo: function(id) {
	  		var i, v, childs
				
				//@d[
				if(arguments.length !== 1)	throw jsl.ParamError()
				jsl.validate(id, "s+", "id")//@d]

				i = this.__infos["i:" + id]//@d[
				if(!i)	throw jsl.ParamError("NotDefined:id")//@d]

				try 			{	v = i.getter ? i.getter.call(this, i.value, id) : i.value 	 	 																					}
				catch(e)	{ throw jsl.ScriptError("BadScript", jsl.Error(e).trace(this.getName() + ".get(v, id)", [ i.value, id ]))	}

				childs = i.childs
				//@d[
        if(childs.length && !jsl.isObject(v)) throw jsl.Error("BadReturnType:Object")//@d]
				
				for(var a = 0; a < childs.length; a++)
					v[childs[a].name] = this.getInfo(childs[a].id)

				return v
		  },
			//-------------------------------------------------------------------------------
			isInfo: function(id) {//@d[
				if(arguments.length !== 1)	throw jsl.ParamError()
				jsl.validate(id, "s+", "id")//@d]
				
				return !!this.__infos["i:" + id]
			},
		  //-------------------------------------------------------------------------------
	 		getInfoData: function(id) {
	 			var i
				//@d[
				if(arguments.length !== 1)		throw jsl.ParamError()
				jsl.validate(id, "s+", "id")//@d]

				i = this.__infos["i:" + id]//@d[
				if(!i)	throw jsl.ParamError("NotDefined:id")//@d]

				return  { 
                  id:     i.id     		, 
                  name:   i.name   		, 
                  value:  i.value  		,
                  getter: i.getter  	,
                  parent: i.parent ? i.parent.id : null 
                }
	 		}
		})
